/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author corneliugosa
 */
@Entity
@Table(name = "person")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),
    @NamedQuery(name = "Person.findStudentsByClassNumber", query = "SELECT ptc.idPerson FROM PersonToClass ptc WHERE ptc.cls.number = :nr AND ptc.idPerson.idType.id = 1")
})
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "name")
    private String name;
    @JoinColumn(name = "id_type", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Type idType;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idStudent")
    private List<ToDoHomework> toDoHomeworkList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idProffessor")
    private List<Timetable> timetableList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idProffessor")
    private List<Course> courseList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idProffessor")
    private List<Feedback> feedbackList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "person")
    private List<LinkCatalogue> linkCatalogueList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idProffessor")
    private List<Homework> homeworkList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idPerson")
    private List<PersonToClass> personToClassList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "proffessor")
    private List<Exams> examsList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "person")
    private List<User> userList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idPerson")
    private List<LinkProfessor> linkProfessorList;
 
    public Person() {
    }

    public Person(Integer id) {
        this.id = id;
    }

    public Person(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getIdType() {
        return idType;
    }

    public void setIdType(Type idType) {
        this.idType = idType;
    }

    @XmlTransient
    public List<ToDoHomework> getToDoHomeworkList() {
        return toDoHomeworkList;
    }

    public void setToDoHomeworkList(List<ToDoHomework> toDoHomeworkList) {
        this.toDoHomeworkList = toDoHomeworkList;
    }

    @XmlTransient
    public List<Timetable> getTimetableList() {
        return timetableList;
    }

    public void setTimetableList(List<Timetable> timetableList) {
        this.timetableList = timetableList;
    }

    @XmlTransient
    public List<Course> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    @XmlTransient
    public List<Feedback> getFeedbackList() {
        return feedbackList;
    }

    public void setFeedbackList(List<Feedback> feedbackList) {
        this.feedbackList = feedbackList;
    }

    @XmlTransient
    public List<LinkCatalogue> getLinkCatalogueList() {
        return linkCatalogueList;
    }

    public void setLinkCatalogueList(List<LinkCatalogue> linkCatalogueList) {
        this.linkCatalogueList = linkCatalogueList;
    }

    @XmlTransient
    public List<Homework> getHomeworkList() {
        return homeworkList;
    }

    public void setHomeworkList(List<Homework> homeworkList) {
        this.homeworkList = homeworkList;
    }

    @XmlTransient
    public List<PersonToClass> getPersonToClassList() {
        return personToClassList;
    }

    public void setPersonToClassList(List<PersonToClass> personToClassList) {
        this.personToClassList = personToClassList;
    }

    @XmlTransient
    public List<Exams> getExamsList() {
        return examsList;
    }

    public void setExamsList(List<Exams> examsList) {
        this.examsList = examsList;
    }

    @XmlTransient
    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    @XmlTransient
    public List<LinkProfessor> getLinkProfessorList() {
        return linkProfessorList;
    }

    public void setLinkProfessorList(List<LinkProfessor> linkProfessorList) {
        this.linkProfessorList = linkProfessorList;
    }
    
    public List<Class> getClasses(){
        List<Class> list = new ArrayList<Class>();
                
        for(PersonToClass ptc: personToClassList){
            list.add(ptc.getCls());
        }
        
        return list;        
    }
    
    public Set<SubjectMatter> getSubjectMatterList(){
        Set<SubjectMatter> list = new HashSet<SubjectMatter>();
        
        for(Timetable time:timetableList){
            list.add(time.getIdSubjectMatter());
        }
        
        return list;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Person[ id=" + id + " ]";
    }
    
}
